<template>
	<div>

		<div class="app-container">

			<TopInfo />

			<Search :lookup="data.lookup" @investigate="inves" />

			<div>
				<el-button v-prmis="'error_code_exportErrorCode'" type="primary" size="small" @click="getExport">
					<template #default>
						<span class="text-sm">{{ $t('public.export') }} Excel</span>
					</template>
				</el-button>
			</div>

		</div>

		<div class="app-container" v-loading="data.mtloading">

			<el-table :data="data.list" :header-cell-style="{ 'text-align': 'center' }"
				:cell-style="{ 'text-align': 'center' }" class="w-full" height="700">
				<el-table-column prop="machine_id" :label="$t('machine.code')" />
				<el-table-column prop="machine_name" :label="$t('machine.name')" />
				<el-table-column prop="error_positionCn" :label="$t('eventlog.system.errorLocation')">
					<template #default="scope">
						{{ setErrorPosition(scope.row) }}
					</template>
				</el-table-column>
				<el-table-column prop="errorCodeType" :label="$t('eventlog.system.category')" />
				<el-table-column prop="errorCode" :label="$t('eventlog.system.errorType')" />
				<el-table-column prop="remark" :label="$t('eventlog.system.briefv')" />
				<el-table-column prop="msg" :label="$t('eventlog.system.msg')"  width="300">
					<!-- <template #default="scope">
						<el-icon :color="scope.row.msg ? '#409Eff' : ''" @click="onParams(scope.row)">
							<Document />
						</el-icon>
					</template> -->
				</el-table-column>
				<el-table-column prop="create_time" :label="$t('date.reportTime')">
					<template #default="scope">
						{{ getPlevel.gettime(scope.row.create_time, 3) }}
					</template>
				</el-table-column>
			</el-table>
			<PageNum :total="data.total" :page="data.info" @exposeData="current" @fraction="fraction" />
		</div>
		<exports :dialogVisible="dialogExport" @close="dialogExport = false" />
		<!-- 详情 -->
		<el-dialog v-model="isParams" :title="$t('public.details')" center width="800" destroy-on-close @close="isParams = false" append-to-body>
			<span>{{ paramsList }}</span>
		</el-dialog>
	</div>
</template>

<script lang="ts" setup>
import { useI18n } from 'vue-i18n' 
const { t: $t } = useI18n()
import getPlevel from "@/utils/level";
import TopInfo from '@/components/TopInfo/index.vue';
import Search from '@/components/Search/index.vue';
import PageNum from '@/components/PageNum/index.vue';
import { getErrorCodeList, exportErrorCode, getTypeList } from '@/api/log'
import { ElMessage, ElMessageBox } from "element-plus";
import exports from '@/components/export.vue';
import { setLastmonth, setThismonth } from '@/utils/index'
import { onMounted } from "vue";
const dialogExport = ref(false)
let paramsList = ref([])
const isParams = ref(false)
import store from '@/store'
let data = reactive({
	typeList: [],
	mtloading: false, // 加载
	lookup: [ // 筛选栏
		{ title: $t('machine.code'), type: 'mId', value: '', key: 'm_id' },
		{ title: $t('eventlog.system.errorLocation'), type: 'define', select: [{ label: $t('machine.operations.mechanism'), value: 1 }, { label: $t('machine.operations.Control'), value: 2 }, { label: $t('machine.operations.computer'), value: 3 }], value: '', key: 'error_position' },
		{ title: $t('date.inventoryTime'), type: 'time', value: '', key: 'time' },
		{ title: $t('eventlog.system.errorType'), type: 'input', value: '', key: 'errorCode' }
	],
	list: [], // 列表信息
	info: { // 筛选数据
		page: 1,
		pageNum: 15,
		machine_id: '',
		error_position: '',
		errorCode: '',
		create_time: ''
	},
	total: 0, // 总条数
})
onMounted(() => {
	findTypeList()
	store.state.menu.forEach(item => {
		if(item.url == '/management/machine.machine_error_code/getTypeList' && item.is_auth == 1){
			data.lookup.splice(1,0,{ title: $t('eventlog.system.category'), type: 'prmis', select: [], value: '', key: 'errorCodeType' })
		}
	});
	let pay_time = ''
	let dates = [];
	dates[0] = setLastmonth()
	dates[1] = setThismonth()
	data.lookup[3].value = dates
	pay_time = setLastmonth() + ' ' + '00:00:00' + '~' + setThismonth() + ' ' + '23:59:59'
	getlist(pay_time)
})
let onParams = (row) => {
  if (row.msg) {
    isParams.value = true
    paramsList.value = row.msg
  }
}
let findTypeList = () => {
	getTypeList({}).then(res=>{
		let typeList = []
		res.data.forEach( item => {
			typeList.push({
				label: item.name,
				value: item.type
			})
		})
		data.lookup.forEach(item => {
			if(item.key == 'errorCodeType'){
				item.select = typeList
			}
		})
	})
}

let setErrorPosition = (row) => {
	if (row.error_position == 1) {
		return row.error_positionCn = $t('machine.operations.mechanism')
	}
	if (row.error_position == 2) {
		return row.error_positionCn = $t('machine.operations.Control')
	}
	if (row.error_position == 3) {
		return row.error_positionCn =  $t('machine.operations.computer')
	}
}
// 获取列表信息
let getlist = (e) => {
	if (e) {
		data.info.create_time = e
	}
	data.mtloading = true
	getErrorCodeList(data.info)
		.then((res) => {
			data.mtloading = false
			if (res.state == 200) {
				data.list = res.data.data;
				data.total = res.data.total
			}
		}).catch((err) => { });
};
// 导出
let getExport = () => {
	let info = {
		machine_id: data.info.machine_id,
		error_position: data.info.error_position,
		errorCode: data.info.errorCode,
		create_time: data.info.create_time
	}
	ElMessageBox.confirm($t('message.isExport')).then(res => {
		exportErrorCode(info).then(res => {
			if (res.state == 200) {
				dialogExport.value = true
				return
			}
			ElMessage({
				message: $t('message.networkFailed'),
				type: 'error'
			})
		}).catch(err => {
			console.log(err)
		})
	})
}
// 查询回调
let inves = (e) => {
	data.info.create_time = ''
	for (let key in e) {
		if (key != 'time') {
			data.info[key] = e[key]
		}
	}
	if (e.time != '') {
		data.info.create_time = e.time[0] + ' ' + '00:00:00' + '~' + e.time[1] + ' ' + '23:59:59'
	}
	data.info.page = 1
	data.info.pageNum = 15
	getlist()
}

// 页数回调
let current = (e) => {
	data.info.page = e
	getlist()
}

// 条数回调
let fraction = (e) => {
	data.info.pageNum = e
	getlist()
}
getlist()

</script>
